var express = require('express');
var db = require('./db.js')

var router = express.Router()
var bodyParser = require('body-parser');
var md5 = require('md5-nodejs');
var urlencodedParser = bodyParser.urlencoded({ extended: false })

var multer = require('multer');

router.get('/list', async function (req, res) {
    let { pages, pagesize,keyword } = req.query
    if(!pages){
        pages=0
    }
    if(!pagesize){
        pagesize=20
    }
    let sql = `select id,title,cid from msbd`;
    let sql2 = `select count(*) as total from msbd`;
    if(keyword){
        sql+=` where cid like '%${keyword}'`
        sql2+=` where cid like '%${keyword}'`
    }
        let num = pages * pagesize
    sql += ` limit ${num},${pagesize}`
    let q1 =await db.doSql(sql)
    let q2 =await db.doSql(sql2)
    res.send(JSON.stringify(db.heal(q1,q2[0].total)))
    
})

//详情页获取当前数据
router.get('/getlist',async function(req,res){
    let { id }=req.query
    let sql =`select m.*,c.name as type from msbd as m,classify as c where m.cid =c.id && m.id=${id}`
    let r =await db.doSql(sql)
    res.send(JSON.stringify(db.heal(r[0])))
})

//获取类别数据
router.get('/gettype',async function(req,res){
    let sql=`select * from classify order by id desc`
    const data=await db.doSql(sql)
    res.send(JSON.stringify(db.heal(data)))
})


module.exports = router
